<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<textarea id="sendMsgText"  name="message"></textarea>
<input type="button" value="发送数据" onclick="sendMsg()">


<textarea id="returnMessage" name="returnMessage"></textarea>

</body>
<script>
    var socket;
    returnMessage = document.getElementById("returnMessage");
    sendMsgText = document.getElementById("sendMsgText");
    if(window.WebSocket){
        socket = new WebSocket("ws://localhost:9999/ws");
        socket.onmessage = function (event) {
            returnMessage.value  = returnMessage.value +"\n"+ event.data;
        };
        socket.onerror = function (ev) {
            returnMessage.value = returnMessage.value +"\n"+ "链接异常";
        };
        socket.onclose = function (ev) {
            returnMessage.value = returnMessage.value +"\n"+ "链接关闭";
        };
        socket.onopen = function (ev) {
            returnMessage.value = returnMessage.value +"\n"+ "链接开启";
        };
    }else {
        alert("不支持webSocket");
    }
    function sendMsg(){
        if(socket.readyState = WebSocket.OPEN){
            socket.send(sendMsgText.value);
        }else{
            alert("链接已断开");
        }
    }
</script>
</html>
